/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestorxml;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import javax.activation.MimetypesFileTypeMap;

/**
 *
 * @author asc
 */
public class FileProcesor extends SimpleFileVisitor<Path> {

    //http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
    private  String DirectoryList;
    private  String FileList;
    private  String ROOT;
    private  int dirs;
    private  int files;
    
    private  HashMap<String, String[]> rows;
    private  HashMap<String, Integer> DirConv;
    private  Object[][] x;

    public FileProcesor(String root)
    {
        dirs = 0;
        files = 0;
        DirConv = new HashMap<String, Integer>();
        rows = new HashMap<String, String[]>();
        DirectoryList="";
        FileList="";
        ROOT = root;
    }
    
    @Override
    public FileVisitResult visitFile(
            Path aFile, BasicFileAttributes aAttrs) throws IOException {
        System.out.println("Processing file:" + aFile);
        
        //DECRAPED DEBUG ONLY
        FileList += aFile + "\n";
        
        //INIT VARS
        String nFile = String.valueOf(files);
        String nFileID = "file" + nFile;
        String id = "f" + nFile;
        String padre = String.valueOf(DirConv.get(aFile.getParent().toString()));
        String father = "d" + padre;
        String name = aFile.getFileName().toString();
        String path = aFile.toString();
        String size = String.valueOf(aFile.toFile().length());
        String type = new MimetypesFileTypeMap().getContentType(aFile.toFile());
        
        //NODE FILE + ATTR(ID FATHER)
        XMLWriter.AppendChild("file", files, "files0");
        XMLWriter.SetAttribute("file" + nFile, "id", id);
        XMLWriter.SetAttribute("file" + nFile, "father", father);
        //SUBNODE NAME
        XMLWriter.AppendChild("name", files, nFileID);
        XMLWriter.AppendTextChild(name, "name" + nFile);
        //SUBNODE PATH
        XMLWriter.AppendChild("path", files, nFileID);
        XMLWriter.AppendTextChild(path, "path" + nFile);
        //SUBNODE SIZE
        XMLWriter.AppendChild("size", files, nFileID);
        XMLWriter.AppendTextChild(size, "size" + nFile);
        //SUBNODE TYPE
        XMLWriter.AppendChild("type", files, nFileID);
        XMLWriter.AppendTextChild(type, "type" + nFile);

        //TABLE ROW INFO
        String[] a = {
            id,     //ID,
            father, //PADRE
            name,   //NOMBRE
            path,   //PATH
            "",     //DIR
            "",     //FI
            size,   //TAM
            type    //TIPO
        };
        rows.put(path, a);
        
        //CAMBIOS OBJETO PADRE
        if (!path.equals(ROOT)) 
        {
            XMLWriter.AppendAtributteInfo("fi" + padre, "x", id);
            rows.get(aFile.getParent().toString())[5]+=id;
        }
        
        //NEXT FILE
        files++;
        
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult preVisitDirectory(
            Path aDir, BasicFileAttributes aAttrs) throws IOException {
        System.out.println("Processing directory:" + aDir);
        
        //DECRAPED DEBUG ONLY
        DirectoryList += aDir + "\n";
        
        //INIT VARS
        String nDir = String.valueOf(dirs);
        String nDirID = "folder" + nDir;
        String id = "d" + nDir;
        String padre = String.valueOf(DirConv.get(aDir.getParent().toString()));
        String father = "d" + padre;
        String name = aDir.getFileName().toString();
        String path = aDir.toString();
        String size = String.valueOf(aDir.toFile().length());
        String type = new MimetypesFileTypeMap().getContentType(aDir.toFile());
        
        //ADD REFERENCE
        DirConv.put(aDir.toString(), dirs);
        
        //CREATE NODE
        XMLWriter.AppendChild("folder", dirs, "folders0");
        XMLWriter.SetAttribute(nDirID, "id", id);
        
        //SUBNODE FATHER
        if (!path.equals(ROOT)) 
        { 
            XMLWriter.SetAttribute(nDirID, "father", father);
        }
        else
        {
            father = "";
        }
        //SUBNODE NAME
        XMLWriter.AppendChild("name", dirs, nDirID);
        XMLWriter.AppendTextChild(name, "name" + nDir);
        //SUBNODE PATH
        XMLWriter.AppendChild("path", dirs, nDirID);
        XMLWriter.AppendTextChild(path, "path" + nDir);
        //SUBNODE DIR
        XMLWriter.AppendChild("dir", dirs, nDirID);
        //SUBNODE FI
        XMLWriter.AppendChild("fi", dirs, nDirID);

        //TABLE ROW INFO
        String[] a = {
            id,     //ID,
            father, //PADRE
            name,   //NOMBRE
            path,   //PATH
            "",     //DIR
            "",     //FI
            size,   //TAM
            type    //TIPO
        };
        rows.put(path, a);
        
        //CAMBIOS OBJETO PADRE
        if (!path.equals(ROOT)) 
        {
            XMLWriter.AppendAtributteInfo("dir" + padre, "x", id);
            rows.get(aDir.getParent().toString())[4]+=id;
        }
        
        //NEXT FILE
        dirs++;
        
        return FileVisitResult.CONTINUE;
    }

    public String getDirectoryList() {
        return DirectoryList;
    }

    public void setDirectoryList(String DirectoryList) {
        this.DirectoryList = DirectoryList;
    }

    public String getFileList() {
        return FileList;
    }

    public void setFileList(String FileList) {
        this.FileList = FileList;
    }

    public String getROOT() {
        return ROOT;
    }

    public void setROOT(String ROOT) {
        this.ROOT = ROOT;
    }

    public int getDirs() {
        return dirs;
    }

    public void setDirs(int dirs) {
        this.dirs = dirs;
    }

    public int getFiles() {
        return files;
    }

    public void setFiles(int files) {
        this.files = files;
    }

    public HashMap<String, Integer> getDirConv() {
        return DirConv;
    }

    public void setDirConv(HashMap<String, Integer> DirConv) {
        this.DirConv = DirConv;
    }

    public HashMap<String, String[]> getRows() {
        return rows;
    }

    public void setRows(HashMap<String, String[]> rows) {
        this.rows = rows;
    }
    
    
}
